package com.whu.waterworks.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.whu.waterworks.dao.WaterUserDAO;
import com.whu.waterworks.model.MyUserDetails;
import com.whu.waterworks.model.WaterUser;

public class WaterUserService implements UserDetailsService{
	private WaterUserDAO waterUserDAO;
	@Override
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException {
		//System.out.println("username:"+userName);
		WaterUser waterUser =waterUserDAO.findByName(userName);
		if(waterUser==null)
		{
			throw new UsernameNotFoundException(WaterUserService.class.getName());
		}
		else {
			MyUserDetails userDetails=new MyUserDetails(waterUser);
			System.out.println("username:"+userDetails.getUsername());
			System.out.println("Authorities:"+userDetails.getAuthorities().toString());
		    return userDetails;
		}
	}
	public WaterUser getUserByName(String userName) {
		return waterUserDAO.findByName(userName);
	}
	public WaterUserDAO getWaterUserDAO() {
		return waterUserDAO;
	}
	public void setWaterUserDAO(WaterUserDAO waterUserDAO) {
		this.waterUserDAO = waterUserDAO;
	}

}
